package com.xuyiwei.xblog.business.article.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xuyiwei.xblog.business.article.dao.ArticleTagsMapper;
import com.xuyiwei.xblog.business.article.model.ArticleTags;
import com.xuyiwei.xblog.business.article.service.IArticleTagsService;
import com.xuyiwei.xblog.core.base.response.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 文章标签 服务实现类
 * </p>
 *
 * @author xuyiwei
 * @since 2018-10-29
 */
@Service
public class ArticleTagsServiceImpl extends ServiceImpl<ArticleTagsMapper, ArticleTags> implements IArticleTagsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeByArticleId(Long articleId) {
        EntityWrapper<ArticleTags> wrapper = new EntityWrapper<>();
        wrapper.eq("article_id",articleId);
        baseMapper.delete(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertList(Long[] tags, Long articleId) {
        for(Long tag :tags){
            ArticleTags articleTags = new ArticleTags();
            articleTags.setArticleId(articleId);
            articleTags.setCreateTime(new Date());
            articleTags.setTagId(tag);
            articleTags.setUpdateTime(new Date());
            baseMapper.insert(articleTags);
        }
    }
}
